import java.io.File;
import java.util.Scanner;

public class IODemo15 {
    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        System.out.println("请输入要扫描的路径：");
        String basePath = scanner.next();

        File dir = new File(basePath);
        if (!dir.exists()) {
            System.out.println("输入的扫描路径不存在，请确定输入的路径是否正确！");
            return;
        }
        if (dir.isFile()) {
            System.out.println("输入的扫描路径为文件，请确定输入的路径是否正确！");
            return;
        }

        System.out.println("请输入要删除的文件名：");
        String fileToDelete = scanner.next();

        scanDir(dir, fileToDelete);
    }

    private static void scanDir(File dir, String fileToDelete) {
        File[] files = dir.listFiles();
        if (files == null) {
            return;
        }
        for (File f: files) {
            if(f.isDirectory()){
                scanDir(f,fileToDelete);
            }else{
                if(f.getName().contains(fileToDelete)){
                    System.out.println("请确定是否删除该文件：" + f.getAbsolutePath());

                    String choice = scanner.next().toLowerCase();
                    if(choice.equals("y")){
                        f.delete();
                        System.out.println("删除成功！");
                    }else{
                        System.out.println("删除失败！");
                    }
                }
            }
        }
    }
}
